<?php 
//File ini untuk mengedit record
require_once('lib/dbclass.php');
$db = new MySQL();

// ubah data tabel berita
$x = $_GET['id'];
if(isset($_POST['ubah'])){	
	
	
	$jdl = $_POST['jdl'];
	$pengirim = $_POST['pengirim'];
	$kat = $_POST['kat'];
	$isi = $_POST['isi'];
	$time= date('Y-m-d H:m:s');
	$tipe = $_POST['tipe'];

	if (! $db->Query("UPDATE berita set id_kategori='$kat',pengirim='$pengirim',judul='$jdl'
		, isi_berita='$isi', jenis='$tipe', tanggal='$time' 
		WHERE id_berita='$x'")) $db->Kill(); 

	echo '<script>';
	echo "alert('Success: Anda berhasil merubah berita');";
	echo "window.location='index.php?q=3'";
	echo '</script>';
}
?>

<?php 
	//Select Data Berita 
	if(isset($_GET['id'])){		
		if (! $db->Query("SELECT * FROM berita WHERE id_berita=$x")) $db->Kill(); 
		if($db->RowCount()>0){
			$rs = $db->RowArray();
		}
	}
?>
	
<?php 
	//select kategori berita sesuai yg di table berita
	$id_kategori = $rs['id_kategori'];
	if (! $db->Query("SELECT * FROM kategori WHERE id_kategori='$id_kategori'")) 
		$db->Kill(); 
	
	if($db->RowCount()>0){
		$kat_selected = $db->RowArray();
	}
?>
<h1>Edit Data Contents</h1><br/>
		
		<form class="form-horizontal" role="form" action="" method="post">
			
			<div class="form-group">
				
				<label for="pengirim" class="col-sm-2 control-label"> Pengirim   : </label>
				<div class="col-sm-10">
				<input class="form-control" type="text" name="pengirim" id="pengirim" value="<?php echo $_SESSION['session_usr'];?>" placeholder="Pengirim">
				
			</div>
			</div>

			<div class="form-group">
				
				<label for="judul" class="col-sm-2 control-label">Judul      :</label>
				<div class="col-sm-10">
				<input class="form-control" type="text" name="jdl" id="jdl" value="<?php echo $rs['judul'];?>" placeholder="Judul">
				
			</div>
			</div>
			
			<div class="form-group">
				
				<label for="kat" class="col-sm-2 control-label">Kategori   :</label>
				<div class="col-sm-10">
				<select class="form-control" name="kat" id="kat">
					
					<option value="<?php echo $kat_selected[0]?>">
						<?php echo $kat_selected[1];?></option>
					        <?php 
							//select semua kategori
							if (! $db->Query("SELECT * FROM kategori where jenis='berita'")) $db->Kill(); 
							?>
					        <?php while($kat_all = $db->RowArray()):?>
				          <option value='<?php echo $kat_all[0]?>'><?php echo $kat_all[1]?>
					         </option>
					          <?php endwhile;?>		
				</select>
				</div>
	
			</div>
				<div class="form-group">
					
				<label for="body" class="col-sm-2 control-label">Isi Berita :</label>
				<div class="col-sm-10">
				<textarea class="form-control editor" name="isi" id="isi" rows="12" placeholder="Page Body"><?php echo $rs[4];?></textarea>
				</div>
			</div>
			
			<div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <div class="checkbox">
		        <label>
		        	<input type='radio' value='Publik' name='tipe' 
						<?php if($rs[7]=='Public') {?> checked="checked" <?php }?>/>
					Publik
					<input type='radio' value='Privat' name='tipe' 
						<?php if($rs[7]=='Privat') {?> checked="checked" <?php }?>/>
					Privat
		        </label>
		    </div>
		    </div>
		   	</div>
			
			<div class="form-group">
					
				<label for="body" class="col-sm-2 control-label"></label>
				<div class="col-sm-10">
					&nbsp;
				</div>
			</div>
			
			<div class="form-group">
   			<div class="col-sm-offset-2 col-sm-10">
			<button type="submit" class="btn btn-success">Save</button>
			<input type="hidden" name="ubah" value="Ubah">
			</div>
			</div>
			
		</form>
	